package com.system.Service.Impl;
import com.common.Utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.Entity.Role;
import com.system.Mapper.RoleMapper;
import com.system.Service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo<Role> getRoleList(Integer pageNum, Integer pageSize, String roleName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roleList = roleMapper.getRoleList(roleName);
        return new PageInfo<>(roleList);
    }

    @Override
    public void addRole(Role role) {
        //创建人
        String token=role.getToken();
        String userName= JwtUtil.extractUsername(token);
        role.setCreateUser(userName);

        LocalDateTime dateTime = LocalDateTime.now();
        role.setCreateTime(dateTime);

        roleMapper.addRole(role);

    }

    @Override
    public void updateRole(Role role) {

        String token=role.getToken();
        String userName= JwtUtil.extractUsername(token);
        role.setUpdateUser(userName);
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }

    @Override
    public void deleteRole(String roleId) {
        roleMapper.deleteRole(roleId);

    }

    @Override
    public List<Role> getRoleLists() {
        return roleMapper.getRoleLists();
    }
}
